# -*- coding: utf-8 -*-
# @Time    : 2018-05-21 20:22
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : LoanLimitUsedSituation.py
# @Software: PyCharm

'''eof
name:贷款及其他账户额度使用比例
code:LoanLimitUsedSituation
tableName:
columnName:
groups:个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData


def LoanLimitUsedSituation():
    try:
        # 未结清贷款信息汇总
        icrUnpaidLoan = FormatData("ICRUnpaidLoan")
        # 未销户贷记卡信息汇总
        icrUndestoryLoancard = FormatData("ICRUndestoryLoancard")
        # 未销户准贷记卡信息汇总
        icrUndestoryStandardLoancard = FormatData("ICRUndestoryStandardLoancard")
        if icrUnpaidLoan == [{}] and icrUndestoryLoancard == [{}] and icrUndestoryStandardLoancard ==[{}]:
            return u"缺失值"
        else:
            # 未结清贷款余额
            balanceUl = ConvertDataType(icrUnpaidLoan[0].get("Balance"), 'float')
            # 合同总额
            creditLimitUl = ConvertDataType(icrUnpaidLoan[0].get("CreditLimit"), 'float')
            creditLimitUl = None if balanceUl is None else creditLimitUl
            balanceUl = None if creditLimitUl is None else balanceUl
            # 已用额度
            usedCreditLimitUlc = ConvertDataType(icrUndestoryLoancard[0].get("UsedCreditLimit"), 'float')
            # 授信总额
            creditLimitUlc = ConvertDataType(icrUndestoryLoancard[0].get("CreditLimit"), 'float')
            usedCreditLimitUlc = None if creditLimitUlc is None else usedCreditLimitUlc
            creditLimitUlc = None if usedCreditLimitUlc is None else creditLimitUlc
            # 透支余额
            usedCreditLimitUslc = ConvertDataType(icrUndestoryStandardLoancard[0].get("UsedCreditLimit"), 'float')
            creditLimitUslc = ConvertDataType(icrUndestoryStandardLoancard[0].get("CreditLimit"), 'float')
            usedCreditLimitUslc = None if creditLimitUslc is None else usedCreditLimitUslc
            creditLimitUslc = None if usedCreditLimitUslc is None else creditLimitUslc

            if balanceUl is None and usedCreditLimitUlc is None and usedCreditLimitUslc is None:
                return u"缺失值"
            if creditLimitUl is None and creditLimitUlc is None and creditLimitUslc is None:
                return u"缺失值"

            usedCreditLimit = ConvertDataType(usedCreditLimitUlc, 'None') + ConvertDataType(balanceUl, 'None')\
                            +ConvertDataType(usedCreditLimitUslc, 'None')
            creditLimit = ConvertDataType(creditLimitUlc, 'None')+ConvertDataType(creditLimitUslc, 'None')\
                          +ConvertDataType(creditLimitUl, 'None')

            if creditLimit == 0:
                return u"缺失值"
            return round(usedCreditLimit/creditLimit, 4)
    except:
        return u"缺失值"


result = LoanLimitUsedSituation()